#!/usr/bin/env python

import wx
import resource

import __main as main

class Main(wx.Frame):
    '''Main object
    
    @author Julot <andy.julot@gmail.com>
    @since Saturday, January 14, 2012. 09:01 AM'''
    
    def __init__(self):
        super(Main, self).__init__(None, name='Main')
        import app
        meta = app.Meta()
        self.Title = '%s Version %s' % (meta.name, meta.long_version)
        self.Icon = resource.icon.Main.Icon
        self._realize()
    
    def _realize(self):
        self.MenuBar = main.MenuBar()
        self.ToolBar = main.ToolBar(self)
        self.ToolBar.Realize()
        self.StatusBar = wx.StatusBar(self)
        
        panel = wx.Panel(self)
        
        self.splitter = wx.SplitterWindow(panel, style=wx.SP_LIVE_UPDATE)
        
        panelLeft = wx.Panel(self.splitter)
        self.search = wx.SearchCtrl(panelLeft, style=wx.TE_PROCESS_ENTER)
        self.search.ShowCancelButton(True)
        self.titles = main.Titles(panelLeft)
        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(self.search, 0, wx.GROW)
        sizer.AddSpacer(1)
        sizer.Add(self.titles, 1, wx.GROW)
        panelLeft.SetSizer(sizer)
        
        self.anime = main.Anime(self.splitter)
        
        self.splitter.SetMinimumPaneSize(20)
        self.splitter.SplitVertically(panelLeft, self.anime)
        
        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(self.splitter, 1, wx.GROW)
        panel.SetSizer(sizer)
        
        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(panel, 1, wx.GROW)
        self.SetSizerAndFit(sizer)
    
    _busyInfo = None
    
    def busyInfo(self, message):
        cursor = wx.StockCursor(wx.CURSOR_WAIT)
        self.SetCursor(cursor)
        self._busyInfo = wx.BusyInfo(message, self)
        wx.Yield()
        return self._busyInfo
    
    def idle(self):
        if self._busyInfo: del self._busyInfo
        if self._progressDialog: self._progressDialog.Destroy()
        cursor = wx.StockCursor(wx.CURSOR_ARROW)
        self.SetCursor(cursor)
    
    _progressDialog = None
    
    def progressDialog(self, title, message, maximum=100,
                       style=wx.PD_AUTO_HIDE|wx.PD_APP_MODAL):
        cursor = wx.StockCursor(wx.CURSOR_WAIT)
        self.SetCursor(cursor)
        self._progressDialog = wx.ProgressDialog(title, message, maximum, self,
                                                 style)
        self._progressDialog.Show()
        return self._progressDialog
    
    def updateProgress(self, value, message):
        return self._progressDialog.Update(value, message)
    
    def resumeProgress(self):
        self._progressDialog.Resume()
    
    def messageDialog(self, message, caption, style=wx.OK,
                      pos=wx.DefaultPosition):
        dialog = wx.MessageDialog(self, message, caption, style, pos)
        result = dialog.ShowModal()
        dialog.Destroy()
        return result
    
    def fileDialog(self, message="Choose a file", defaultDir="",
                   defaultFile="", wildcard="*.*", style=wx.FD_DEFAULT_STYLE,
                   pos=wx.DefaultPosition, sz=wx.DefaultSize):
        dialog = wx.FileDialog(self, message, defaultDir, defaultFile, wildcard,
                               style, pos, sz)
        result = dialog.ShowModal()
        dialog.Destroy()
        return dialog.Path if result == wx.ID_OK else None
    